<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Home\IndexController;
use DB;
use Hash;
use Illuminate\Support\Facades\Mail;
use App\Models\User;
use App\SMS\SendTemplateSMS;
use App\SMS\M3Result;
use Illuminate\Support\Facades\Crypt;
use App\Http\Requests\Dorepass;
class LoginController extends Controller
{
    /**
     * 加载前台登录页视图
     * @return
     */
    public function index()
    {
        return view('home.login.index');
    }
    /**
     * 处理前台登录
     * @return
     */
    public function do_login(Request $request)
    {

    	//获取用户输入的值
    	$arr=$request->all();

    	//查询数据库的值
    	$data = DB::table('zy_user')->where('username','=',$arr['username'])->first();
        // dd($data);
    	if($data['username']==null)
    	{
    		echo "<script>alert('用户名不能为空');location.href='/home'</script>";
    	}
    	if($arr['username'] != $data['username'])
    	{
    		echo "<script>alert('用户名不正确');location.href='/home'</script>";
    	}

        // 判断密码
        if(Hash::check($arr['password'], $data['password'])){


                    session(['login'=>$data['id']]);
                    session(['username'=>$data['username']]);

                    return redirect('/')->with('success','登陆成功');


                }else{


                    return back()->with('error','密码验证失败');
                }

    }

    /**
     * 登陆正则验证用户
     * @return
     */
    public function dluser(Request $request)
    {
        $username=$request->input('username');
        $data=DB::table('zy_user')->where('username','=',$username)->first();
        //  判断用户登陆权限
        $user_status = $data['status'];

        if($data)
        {

            if($user_status == 1){
                // 账号冻结
                echo 1;
            }else{
                // 用户存在可登陆
                echo 0;
            }

        }else{
            // 不存在
            echo 2;
        }
    }

    /**
     * 登陆正则验证密码
     * @return
     */
    public function dlpass(Request $request)
    {
        $username=$request->input('username');
        $password=$request->input('password');
        $data=DB::table('zy_user')->where('username','=',$username)->first();

        if(Hash::check($password, $data['password']))
        {
            //0为相等
            echo 0;
        }else{
            //1为不等
            echo 1;
        }
    }

    //加载前台用户忘记密码页面
    public function forget_pass()
    {
        return view('home.login.forget_pass');
    }

    //用ajax验证是否有用户邮箱
    public function ajax_pass(Request $request)
    {
        $data=$request->all();
        $email=DB::table('zy_user')->where('email',$data['email'])->first();
       if($email['email'])
       {
        return 0;
       }else{
        return 1;
       }
    }

    //邮箱的验证
    public function do_email(Request $request)
    {
      $data=$request->all();
      //获取验证码
        $code1 = session('code');
        $code2=$data['code'];
        //验证验证码是否存在
        if($code1 != $code2)
        {
            echo "<script>alert('验证码输入有错');location.href='/home/forget_pass'</script>";
        }
        //查询邮箱
        $user=User::where('email',$data['email'])->first();
        // dd($user->email);
        // dd($data['email']);
        //验证邮箱是否存在
        if($data['email'] == $user['email'])
        {
           // 2 给这个邮箱发密码重置邮件
              Mail::send('home.login.pass_reset', ['user' => $user], function ($m) use ($user) {

             $m->to($user->email,$user->username)->subject('重置密码!');
            });
              echo "<script>alert('邮箱发送成功')</script>";
              return view('home.login.show');
        }else{
             echo "<script>alert('邮箱输入有错');location.href='/home/forget_pass'</script>";
        }

    }

    //加载前台重置密码页面
    public function restart_pass(Request $request)
    {
        $user_id = $request['user'];
       $user = User::find($user_id);
        return view('home.login.new_pass',compact('user'));
    }

    //执行用户密码的修改
    public function do_restart_pass(Dorepass $request)
    {
        // 1 找到要修改密码的用户
        $id = $request->input('user_id');
        $data=$request->all();
        $pass=HASH::make($data['password']);
        $re=DB::table('zy_user')->where('id',$id)->update(['password'=>$pass]);
        if($re)
        {
            return redirect('/home')->with('success','重置密码成功');
        }else{
            return back()->with('error','重置密码失败');
        }
    }

}
